   // ****************************************************** 
   //
   // Copyright 2011 OpenAjax Alliance
   //
   // Licensed under the Apache License, Version 2.0 (the "License");
   // you may not use this file except in compliance with the License.
   // You may obtain a copy of the License at
   //
   //    http://www.apache.org/licenses/LICENSE-2.0
   //
   // Unless required by applicable law or agreed to in writing, software
   // distributed under the License is distributed on an "AS IS" BASIS,
   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   // See the License for the specific language governing permissions and
   // limitations under the License.
   //
   // ****************************************************** 

var OAA_CI    = OAA_CI || {}; 
OAA_CI.elements = OAA_CI.elements || {}; 

/** ================================================================
*
* elements Module initialization
*
*
* =============================================================== */

OAA_CI.elements_last_index = 0;

/** ================================================================
*
* onLoad
*
* @desc Initializes elements module
*
* @return nothing
*
* =============================================================== */

OAA_CI.elements.onLoad = function(oaa_cache_inspector) {
  
  OAA_CI.elements_nls_strings = document.getElementById("ID_STRINGBUNDLE_ELEMENTS");        
  
  OAA_CI.elements.onShowListOfElementsCommand();
}; 

/** ================================================================
*
* onShowListOfElementsCommand
*
* @desc Evaluates the document and popluates the XUL controls with DOMElements using a list
*
* @return nothing
*
* =============================================================== */

OAA_CI.elements.onShowListOfElementsCommand = function(e) {

  var i;
  var de;

  OAA_CI.evaluateDocument();   
  
  var element_cache = OAA_CI.evaluation.dom_cache.element_cache;  
  element_cache.sortDOMElements('document_order', true);
  var dom_elements = element_cache.dom_elements;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_ELEMENTS_LIST');
  
  OAA_CI.removeChildNodes(xul_treechildren);

  var dom_elements_len = dom_elements.length;
  
  // OpenAjax.a11y.console("Number of list dom elements: " + dom_elements_len);
  
  for (i=0; i<dom_elements_len; i++) {
    de = dom_elements[i];
    OAA_CI.addTreeItem(xul_treechildren, [de.tag_name, de.document_order, de.id, de.class_name, de.xpath], de.cache_id);
  }  // end loop
    
  OAA_CI.updateXULTime(start_time);
    
}; 

/** ================================================================
*
* onShowTreeOfElementsCommand
*
* @desc Evaluates the document and popluates the XUL controls with DOMElements using a tree
*
* @return nothing
*
* =============================================================== */

OAA_CI.elements.onShowTreeOfElementsCommand = function(e) {


  function addDOMElementChildren(xul_treechildren, dom_elements) {

    var de;
    var dom_elements_len = dom_elements.length;
    
    for (var i=0; i<dom_elements_len; i++) {

      de = dom_elements[i]; 

      if (de.type == NODE_TYPE.ELEMENT) {
      
        var xul_treeitem = OAA_CI.createXULElement('treeitem', []);
        
        OAA_CI.addTreeRow(xul_treeitem, [de.tag_name, de.document_order, de.id, de.class_name, de.xpath], de.cache_id);
      
        if (de.children.length) {
      
           xul_treeitem.setAttribute("container", "true");
           xul_treeitem.setAttribute("open", "true");

           var xul_sub_treechildren = OAA_CI.createXULElement('treechildren', []);

           addDOMElementChildren(xul_sub_treechildren, de.children);
         
           xul_treeitem.appendChild(xul_sub_treechildren);
         
         }
         
        xul_treechildren.appendChild(xul_treeitem);
         
       }  
      
    } // loop
  
  } // end function

  OAA_CI.evaluateDocument();   
  
  var i;
  var children = OAA_CI.evaluation.dom_cache.element_cache.children;  
  var children_len = children.length;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_ELEMENTS_TREE');
  
  OAA_CI.removeChildNodes(xul_treechildren);
  
//  OpenAjax.a11y.console("Number of dom cache children: " + children_len);
  
  addDOMElementChildren(xul_treechildren, children);
    
  OAA_CI.updateXULTime(start_time);
    

};



/** ================================================================
*
* onDOMElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the LinkCache
*
* @return nothing
*
* =============================================================== */

OAA_CI.elements.onDOMElementItemsSelectChanged = function (event) {

  var de;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var element_cache = OAA_CI.evaluation.dom_cache.element_cache;  
  var xul_tree = document.getElementById('ID_TREE_ELEMENTS_LIST');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Element cache id: " + cache_id );

    de = element_cache.getDOMElementByCacheId(cache_id);
    
    if (de) {
      OAA_CI.showCacheItemProperties(de);
    }
  } 
  
};  
